package com.example.demo.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.example.demo.model.ExcelDataDo;
import com.example.demo.utils.ExcelListener;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class TestService {

    public List<ExcelDataDo> test(ExcelDataDo inVo) {
        // 读取文件的路径
        String fileName = "D:\\test.xlsx";

        // 创建监听器
        ExcelListener listener = new ExcelListener();

        // 读取 Excel 文件
        EasyExcel.read(fileName, ExcelDataDo.class, listener).sheet().doRead();

        // 获取读取到的数据
        List<ExcelDataDo> dataList = listener.getDataList();
        System.out.println("读取到的数据总数: " + JSON.toJSONString(dataList));

        List<ExcelDataDo> outVos = new ArrayList<>();
        for (ExcelDataDo excelDataDo : dataList) {
            try {
                if ((null != inVo.getInsCode() && excelDataDo.getInsCode().equals(inVo.getInsCode()))
                        || (null != inVo.getInsNm() && excelDataDo.getInsNm().contains(inVo.getInsNm()))
                        || (null != inVo.getScore() && Integer.parseInt(inVo.getScore()) - Integer.parseInt(excelDataDo.getScore()) > 0)) {
                    outVos.add(excelDataDo);
                }
            } catch (NumberFormatException e) {
                System.out.println(e.getMessage());
            }
        }
        return outVos;
    }
}
